package com.xiangban.service;

import com.xiangban.config.AliYunParameterConfig;
import com.xiangban.exception.BizException;
import com.xiangban.service.fegin.SmsServiceFeign;
import com.xiangban.model.dto.SmsParameterDto;
import com.xiangban.model.dto.VerificationDto;
import com.xiangban.model.res.ApiResult;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.service.VerificationService;
import com.xiangban.utils.CommonUtil;
import com.xiangban.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static com.xiangban.utils.CommonUtil.*;
import static com.xiangban.utils.RedisUtil.codeTime;
@Slf4j
@Service
public class VerificationServiceImpl implements VerificationService {
    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SmsServiceFeign smsServiceFeign;
    @Autowired
    private AliYunParameterConfig aliYunParameterConfig;

    public ApiResult<Object> verification(VerificationDto verificationDto) {
        String code = RandomStringUtils.randomNumeric(6);
        //设置模板参数
        HashMap<String, Object> templateParam = new HashMap<>();
        templateParam.put("code",code);
        log.info("手机号：{} 验证码是：{}",verificationDto.getPhone(),code);
        //配置短信参数
        SmsParameterDto smsParameterVo = new SmsParameterDto();
        smsParameterVo.setMobile(verificationDto.getPhone());
        smsParameterVo.setTemplateParam(templateParam);
        //判断验证码类型
        if (common_1.equals(verificationDto.getType())) {
            smsParameterVo.setSmsTemplate(aliYunParameterConfig.getLoginCodeTemplate());
            String key = VerificationDto.verification_type_login + verificationDto.getPhone();
            redisUtil.set(key, code, codeTime);
            commonUtil.invalidVerification(key, verificationDto.getPhone());
            return smsServiceFeign.sendSms(smsParameterVo);
        } else if (common_2.equals(verificationDto.getType())) {
            smsParameterVo.setSmsTemplate(aliYunParameterConfig.getRegisterTemplate());
            String key = VerificationDto.verification_type_register + verificationDto.getPhone();
            redisUtil.set(key, code, codeTime);
            commonUtil.invalidVerification(key, verificationDto.getPhone());
            return smsServiceFeign.sendSms(smsParameterVo);
        } else if (common_3.equals(verificationDto.getType())) {
            smsParameterVo.setSmsTemplate(aliYunParameterConfig.getResetPasswordTemplate());
            String key = VerificationDto.verification_type_rest_password + verificationDto.getPhone();
            redisUtil.set(key, code, codeTime);
            commonUtil.invalidVerification(key, verificationDto.getPhone());
            return smsServiceFeign.sendSms(smsParameterVo);
        }  else {
            throw new BizException(ApiResultCode.PARAMETER_EXCEPTION);
        }
    }





}
